package com.isczy.eduservice.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.excel.EasyExcel;
import com.isczy.eduservice.entity.EduSubject;
import com.isczy.eduservice.entity.vo.ExcelSubjectVo;
import com.isczy.eduservice.entity.vo.SubjectVo;
import com.isczy.eduservice.listener.SubjectExcelListener;
import com.isczy.eduservice.mapper.EduSubjectMapper;
import com.isczy.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author CZY
 * @since 2020-12-31
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /***
     * ***************************************
     * 功能描述:读取excel添加课程分类
     * @param file
     * @return
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2020-12-31 10:29】
     * @author modify:
     */
    @Override
    @Transactional(rollbackFor = MyException.class)
    public void addSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelSubjectVo.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("读取excel添加课程分类失败")
                    .build();
        }finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw MyException.builder()
                            .exception(e)
                            .code(ResultCode.ERROR)
                            .className(getClass().getName())
                            .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                            .message("inputStream close failed!")
                            .build();
                }
            }
        }
    }

    /***
     * ***************************************
     * 功能描述:树形结构课程列表
     * @return SubjectVo
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2020-12-31 10:30】
     * @author modify:
     */
    @Override
    public List<SubjectVo> getAllSubject() {
        return getSubject("0");
    }

    /***
     * ***************************************
     * 功能描述:递归课程列表
     * @return SubjectVo
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2020-12-31 10:30】
     * @author modify:
     */
    private List<SubjectVo> getSubject(String parentId) {
        List<SubjectVo> subjects = baseMapper.selectListByParentId(parentId);
        if (ArrayUtil.isEmpty(subjects)){
            return subjects;
        }
        subjects.forEach(subject ->subject.setChildren(getSubject(subject.getId())));
        return subjects;
    }
}
